using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qWASTAMM = from m in mtemp.WASTAMM select m;
       DBMapper.MapWASTAMM(qWASTAMM.ToList(), true);
       */
        public List<WASTAMM> MapWASTAMM(XElement item2, bool mergedb)
        {
            List<WASTAMM> result = new List<WASTAMM>();
            var item4 = from item3 in item2.Elements()
                        select new WASTAMM
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            WAMANDANT = (string)item3.Element("WAMANDANT"),
                            WAART = (string)item3.Element("WAART"),
                            WAVAR = (string)item3.Element("WAVAR"),
                            WANEUB = (string)item3.Element("WANEUB"),
                            WANEUD = (string)item3.Element("WANEUD"),
                            WABEZ = (string)item3.Element("WABEZ"),
                            WAFDDIM = (string)item3.Element("WAFDDIM"),
                            WARGRQM = (string)item3.Element("WARGRQM"),
                            WABIND = (string)item3.Element("WABIND"),
                            WAFBEM = (string)item3.Element("WAFBEM"),
                            WAFBRTL = (string)item3.Element("WAFBRTL"),
                            WAFGRQM = (string)item3.Element("WAFGRQM"),
                            WAFFDDIK = (string)item3.Element("WAFFDDIK"),
                            WAFFDDIS = (string)item3.Element("WAFFDDIS"),
                            WADART = (string)item3.Element("WADART"),
                            WADVAR = (string)item3.Element("WADVAR"),
                            WAFILLER2 = (string)item3.Element("WAFILLER2"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapWASTAMM(result, mergedb);
          return result;
        }
        ///DBMapper.MapWASTAMM(mtemp.WASTAMM.ToList<WASTAMM>(), true);        
       public void MapWASTAMM(List<WASTAMM> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from WASTAMM row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.WASTAMM.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.WASTAMM.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.WASTAMM.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "WASTAMM")]
    public class WASTAMM : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _WAMANDANT="0";
        [Column]
        public string WAMANDANT
        {
            get
            {
                return _WAMANDANT;
            }
            set
            {
                if (value != _WAMANDANT)
                {
                     _WAMANDANT = value;
                    NotifyPropertyChanged("WAMANDANT");
                }
            }
        }
        private string  _WAART="0";
        [Column]
        public string WAART
        {
            get
            {
                return _WAART;
            }
            set
            {
                if (value != _WAART)
                {
                     _WAART = value;
                    NotifyPropertyChanged("WAART");
                }
            }
        }
        private string  _WAVAR="0";
        [Column]
        public string WAVAR
        {
            get
            {
                return _WAVAR;
            }
            set
            {
                if (value != _WAVAR)
                {
                     _WAVAR = value;
                    NotifyPropertyChanged("WAVAR");
                }
            }
        }
        private string  _WANEUB="0";
        [Column]
        public string WANEUB
        {
            get
            {
                return _WANEUB;
            }
            set
            {
                if (value != _WANEUB)
                {
                     _WANEUB = value;
                    NotifyPropertyChanged("WANEUB");
                }
            }
        }
        private string  _WANEUD="0";
        [Column]
        public string WANEUD
        {
            get
            {
                return _WANEUD;
            }
            set
            {
                if (value != _WANEUD)
                {
                     _WANEUD = value;
                    NotifyPropertyChanged("WANEUD");
                }
            }
        }
        private string  _WABEZ="0";
        [Column]
        public string WABEZ
        {
            get
            {
                return _WABEZ;
            }
            set
            {
                if (value != _WABEZ)
                {
                     _WABEZ = value;
                    NotifyPropertyChanged("WABEZ");
                }
            }
        }
        private string  _WAFDDIM="0";
        [Column]
        public string WAFDDIM
        {
            get
            {
                return _WAFDDIM;
            }
            set
            {
                if (value != _WAFDDIM)
                {
                     _WAFDDIM = value;
                    NotifyPropertyChanged("WAFDDIM");
                }
            }
        }
        private string  _WARGRQM="0";
        [Column]
        public string WARGRQM
        {
            get
            {
                return _WARGRQM;
            }
            set
            {
                if (value != _WARGRQM)
                {
                     _WARGRQM = value;
                    NotifyPropertyChanged("WARGRQM");
                }
            }
        }
        private string  _WABIND="0";
        [Column]
        public string WABIND
        {
            get
            {
                return _WABIND;
            }
            set
            {
                if (value != _WABIND)
                {
                     _WABIND = value;
                    NotifyPropertyChanged("WABIND");
                }
            }
        }
        private string  _WAFBEM="0";
        [Column]
        public string WAFBEM
        {
            get
            {
                return _WAFBEM;
            }
            set
            {
                if (value != _WAFBEM)
                {
                     _WAFBEM = value;
                    NotifyPropertyChanged("WAFBEM");
                }
            }
        }
        private string  _WAFBRTL="0";
        [Column]
        public string WAFBRTL
        {
            get
            {
                return _WAFBRTL;
            }
            set
            {
                if (value != _WAFBRTL)
                {
                     _WAFBRTL = value;
                    NotifyPropertyChanged("WAFBRTL");
                }
            }
        }
        private string  _WAFGRQM="0";
        [Column]
        public string WAFGRQM
        {
            get
            {
                return _WAFGRQM;
            }
            set
            {
                if (value != _WAFGRQM)
                {
                     _WAFGRQM = value;
                    NotifyPropertyChanged("WAFGRQM");
                }
            }
        }
        private string  _WAFFDDIK="0";
        [Column]
        public string WAFFDDIK
        {
            get
            {
                return _WAFFDDIK;
            }
            set
            {
                if (value != _WAFFDDIK)
                {
                     _WAFFDDIK = value;
                    NotifyPropertyChanged("WAFFDDIK");
                }
            }
        }
        private string  _WAFFDDIS="0";
        [Column]
        public string WAFFDDIS
        {
            get
            {
                return _WAFFDDIS;
            }
            set
            {
                if (value != _WAFFDDIS)
                {
                     _WAFFDDIS = value;
                    NotifyPropertyChanged("WAFFDDIS");
                }
            }
        }
        private string  _WADART="0";
        [Column]
        public string WADART
        {
            get
            {
                return _WADART;
            }
            set
            {
                if (value != _WADART)
                {
                     _WADART = value;
                    NotifyPropertyChanged("WADART");
                }
            }
        }
        private string  _WADVAR="0";
        [Column]
        public string WADVAR
        {
            get
            {
                return _WADVAR;
            }
            set
            {
                if (value != _WADVAR)
                {
                     _WADVAR = value;
                    NotifyPropertyChanged("WADVAR");
                }
            }
        }
        private string  _WAFILLER2="0";
        [Column]
        public string WAFILLER2
        {
            get
            {
                return _WAFILLER2;
            }
            set
            {
                if (value != _WAFILLER2)
                {
                     _WAFILLER2 = value;
                    NotifyPropertyChanged("WAFILLER2");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<WASTAMM> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\WASTAMM\\WASTAMM" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\WASTAMM" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("WASTAMM");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (WASTAMM row in ListInput)
                {
                    XElement f = new XElement("WASTAMM");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement WAMANDANT = new XElement("WAMANDANT");
                         WAMANDANT.Value = row.WAMANDANT;
                          f.Add(WAMANDANT);
                        XElement WAART = new XElement("WAART");
                         WAART.Value = row.WAART;
                          f.Add(WAART);
                        XElement WAVAR = new XElement("WAVAR");
                         WAVAR.Value = row.WAVAR;
                          f.Add(WAVAR);
                        XElement WANEUB = new XElement("WANEUB");
                         WANEUB.Value = row.WANEUB;
                          f.Add(WANEUB);
                        XElement WANEUD = new XElement("WANEUD");
                         WANEUD.Value = row.WANEUD;
                          f.Add(WANEUD);
                        XElement WABEZ = new XElement("WABEZ");
                         WABEZ.Value = row.WABEZ;
                          f.Add(WABEZ);
                        XElement WAFDDIM = new XElement("WAFDDIM");
                         WAFDDIM.Value = row.WAFDDIM;
                          f.Add(WAFDDIM);
                        XElement WARGRQM = new XElement("WARGRQM");
                         WARGRQM.Value = row.WARGRQM;
                          f.Add(WARGRQM);
                        XElement WABIND = new XElement("WABIND");
                         WABIND.Value = row.WABIND;
                          f.Add(WABIND);
                        XElement WAFBEM = new XElement("WAFBEM");
                         WAFBEM.Value = row.WAFBEM;
                          f.Add(WAFBEM);
                        XElement WAFBRTL = new XElement("WAFBRTL");
                         WAFBRTL.Value = row.WAFBRTL;
                          f.Add(WAFBRTL);
                        XElement WAFGRQM = new XElement("WAFGRQM");
                         WAFGRQM.Value = row.WAFGRQM;
                          f.Add(WAFGRQM);
                        XElement WAFFDDIK = new XElement("WAFFDDIK");
                         WAFFDDIK.Value = row.WAFFDDIK;
                          f.Add(WAFFDDIK);
                        XElement WAFFDDIS = new XElement("WAFFDDIS");
                         WAFFDDIS.Value = row.WAFFDDIS;
                          f.Add(WAFFDDIS);
                        XElement WADART = new XElement("WADART");
                         WADART.Value = row.WADART;
                          f.Add(WADART);
                        XElement WADVAR = new XElement("WADVAR");
                         WADVAR.Value = row.WADVAR;
                          f.Add(WADVAR);
                        XElement WAFILLER2 = new XElement("WAFILLER2");
                         WAFILLER2.Value = row.WAFILLER2;
                          f.Add(WAFILLER2);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
